//////////////////////////////////////////////////////////////////////////////
// Twitter functionality
//////////////////////////////////////////////////////////////////////////////

(function($){
  
  var regexp  = {
    url: /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?"""'']))/gi      
  };
  
  String.prototype.autolink = function(){
    var _slice = Array.prototype.slice, key, attrs, options, pattern, value, ref;
    options = 1 <= arguments.length ? _slice.call(arguments, 0) : [];
    pattern = regexp.url;
    if( options.length ){
      attrs = '';
      ref  = options[0];
      for( key in _ref ){
        value  = ref[key];
        atttrs+= " " + key + "='" + value + "'";
      }
      return this.replace(pattern, "<a href='$1' " + dcl.trim(attrs) + " target='_blank'>$1</a>");
    }else{
      return this.replace(pattern, "<a href='$1' target='_blank'>$1</a>");
    }
  };
  
  var Twitter = function( el, conf ){
    var element = $(el),
        holder  = $("<p class='tweet'></p>"),
        tweets  = [],
        self    = this,
        cycle, options;
      
    options = {
      url: "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=dclottery&count=5&include_entities=false",
      delay: 8000 
    };
    
    if( conf ) options = $.extend({}, options, conf);
    
    $.extend(self, {
      play: function(){
        clearTimeout(cycle);
        holder.fadeOut(500, function(){
          var next = tweets.shift();
          tweets.push(next);
          holder.html(next);
          holder.fadeIn(500, function(){
            cycle = setTimeout(self.play, options.delay);
          });
        });
      },      
      
      load: function(){
        element.append(holder);
        
        $.ajax({
          url : options.url,
          dataType: 'jsonp',
          success: function(data){
            var next;
            $.each(data, function(){
              tweets.push(this.text.toString().autolink());
            });
            next = tweets.shift();
            holder.html( next );
            tweets.push(next);
            cycle = setTimeout(self.play, options.delay);
          }
        });
      }
      
    });
    
  };
  
  $.fn.twitter = function( options ){
    
    if( $(this).data('twitter') ) return this;
    
    this.each(function(){
      var instance = $(this).data('twitter');
      if( !instance ){
        instance = new Twitter( $(this), options);
        instance.load();
      }
      
      $(this).data('twitter', instance);
      
    });
    
    return this;
    
  };
  
  $(function(){
    
    $('*[data-plugin="twitter"]').twitter();
      // 
      // if( marquee.length ){
      //   $($('#marquee_nav_tpl').html()).appendTo(marquee).hide();
      //   marquee.marquee({ slides: 'section.slide', navigation: $("#marquee_nav") });  
      // }
  });
  
})(jQuery);